﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Vara_Data_Layer.Vara_Data_Asset;
using System.ComponentModel;

public partial class _Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    //protected void ButtonTest_Click(object sender, EventArgs e)
    //{
    //    Asset_Modal1.Show(int.Parse(TextBoxAssetNumber.Text));
    //}
    protected void ButtonNewAsset_Click(object sender, EventArgs e)
    {
        Asset_Modal1.Show(0);
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        HideSearchMessage();
        if (TextBoxSearchAsset.Text == "")
        {
            DisplayErrorNoSearchItemEntered();
        }
        else
        {
            SearchAssetsPaging(1, 30, "ID", TextBoxSearchAsset.Text);
        }
    }
    protected void DisplayErrorNoItemFound()
    {
        LabelSearchDisplay.Text = "No items found";
        LabelSearchDisplay.Visible = true;
    }
    protected void DisplayErrorNoSearchItemEntered()
    {
        LabelSearchDisplay.Text = "Please enter an ID, Asset label or asset name to search for";
        LabelSearchDisplay.Visible = true;
    }
    protected void HideSearchMessage() 
    {
        LabelSearchDisplay.Visible = false;    
    }
    protected void SearchAssetsPaging(int StartIndex, int PageSize, string SortBy, string SearchCriteria)
    {
        Vara_Data_Asset_Controller VaraController = new Vara_Data_Asset_Controller();
        DataTable DataTableSearchResults = VaraController.SearchAssets(SearchCriteria, StartIndex, PageSize, SortBy);

        if (DataTableSearchResults.Rows.Count > 0)
        {
            GridViewSearchResults.DataSource = DataTableSearchResults;
            GridViewSearchResults.DataBind();
        }
        else
        {
            DisplayErrorNoItemFound();
            GridViewSearchResults.DataSource = DataTableSearchResults;
            GridViewSearchResults.DataBind();
        }
    }
    protected void GridViewSearchResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewSearchResults.PageIndex = e.NewPageIndex;
        int rowIndex = e.NewPageIndex + 30;
        SearchAssetsPaging(rowIndex, 30, "ID", TextBoxSearchAsset.Text);

    }
    protected void GridViewSearchResults_OnSorting(object sender, GridViewSortEventArgs e)
    {
        DataTable DataTableOfReturnedResults = GridViewSearchResults.DataSource as DataTable;
        if (DataTableOfReturnedResults != null)
        {
            DataView dataView = new DataView(DataTableOfReturnedResults);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
            GridViewSearchResults.DataSource = dataView;
            GridViewSearchResults.DataBind();
        }
    }
    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string NewSortDirection = string.Empty;
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                NewSortDirection = "ASC";
                break;
            case SortDirection.Descending:
                NewSortDirection = "DESC";
                break;
        }
        return NewSortDirection;
    }
    protected void GridViewSearchResultsCommandControls(object sender, EventArgs e)
    {
        GridViewRow GridViewCurrentRow = ((GridViewRow)((ImageButton)sender).NamingContainer);
        ImageButton ImageButtonSent = ((ImageButton)sender);
        if (ImageButtonSent.CommandName == "EditAsset")
        {
            int AssetToShow = int.Parse(GridViewSearchResults.DataKeys[GridViewCurrentRow.RowIndex].Values[0].ToString());
            Asset_Modal1.Show(AssetToShow);
        }
        else if (ImageButtonSent.CommandName == "DeleteAsset")
        {
            //Vara_Data_Asset_Controller AssetController = new Vara_Data_Asset_Controller();
            //AssetController.DeleteAsset(int.Parse(GridViewSearchResults.DataKeys[GridViewCurrentRow.RowIndex].Values[0].ToString()));
            //DataTable DataTableOfReturnedResults = GridViewSearchResults.DataSource as DataTable;
            //GridViewSearchResults.DataBind();
        }

    }
}